|
|
@@ -7,14 +7,12 @@ module Agents
|
7
|
7
|
The Public Transport Agent monitors if any bus is expected to arrive at a particular stop in 5 minutes or less.
|
8
|
8
|
You must specify 5 things for it too work correctly. Your state, city, route, stop and destination. All these things
|
9
|
9
|
should be in the language that nextbus understands. For details check out http://www.nextbus.com/predictor/stopSelector.jsp?a=sf-muni and http://www.apihub.com/nextbus/api/nextbus-api/docs/reference.
|
10
|
|
-
|
|
10
|
+
|
11
|
11
|
Specify the following user settings:
|
12
|
12
|
- stops (array)
|
13
|
13
|
- agency (string)
|
14
|
14
|
- alert_window_in_minutes (integer)
|
15
|
15
|
|
16
|
|
-
|
17
|
|
-
|
18
|
16
|
This Agent generates Events based on NextBus GPS transit predictions. First, select an agency by visiting http://www.nextbus.com/predictor/agencySelector.jsp and finding your transit system. Once you find it, copy the part of the URL after `?a=`. For example, for the San Francisco MUNI system, you would end up on http://www.nextbus.com/predictor/stopSelector.jsp?a=sf-muni and copy "sf-muni". Put that into this Agent's agency setting.
|
19
|
17
|
|
20
|
18
|
Next, find the stop tags that you care about. To find the tags for the sf-muni system, for the N route, visit this URL:
|
|
|
@@ -32,15 +30,13 @@ module Agents
|
32
|
30
|
Finally, set the arrival window that you're interested in. E.g., 5 minutes. Events will be created by the agent anytime a new train or bus comes into that time window.
|
33
|
31
|
|
34
|
32
|
alert_window_in_minutes: 5
|
35
|
|
-
|
36
|
|
-
|
|
33
|
+
|
37
|
34
|
having the agent's default check period be every minute, and creating an Event in #check whenever a new tripTag (supplied by the predictionsForMultiStops API) shows up within alert_window_in_minutes from the stop. Do not create events for the same tripTag more than once per stop. I'd do this by keeping a list of [stop tag, tripTag, timestamp] tuples in memory and checking to make sure one doesn't already exist before making a new Event. This memory should get cleaned up when timestamp is older than an hour (or something) so that it doesn't fill up all of the Agent's memory.
|
38
|
35
|
|
39
|
36
|
The NextBusAgent doesn't need to receive Events.
|
40
|
37
|
|
41
|
38
|
It needs to fetch XML from one URL, store a list of timestamps in memory, and make Events.
|
42
|
39
|
|
43
|
|
-
|
44
|
40
|
MD
|
45
|
41
|
|
46
|
42
|
|
|
|
@@ -50,7 +46,7 @@ It needs to fetch XML from one URL, store a list of timestamps in memory, and ma
|
50
|
46
|
Events look like this:
|
51
|
47
|
{ "routeTitle":"N-Judah",
|
52
|
48
|
"stopTag":"5215",
|
53
|
|
- "prediction":
|
|
49
|
+ "prediction":
|
54
|
50
|
{"epochTime":"1389622846689",
|
55
|
51
|
"seconds":"3454","minutes":"57","isDeparture":"false",
|
56
|
52
|
"affectedByLayover":"true","dirTag":"N__OB4KJU","vehicle":"1489",
|
|
|
@@ -66,31 +62,23 @@ It needs to fetch XML from one URL, store a list of timestamps in memory, and ma
|
66
|
62
|
@session.headers['User-Agent'] = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/534.12 (KHTML, like Gecko) Chrome/9.0.584.0 Safari/534.12"
|
67
|
63
|
@session
|
68
|
64
|
end
|
69
|
|
- def check_me
|
70
|
|
- binding.pry
|
71
|
|
- end
|
72
|
|
- def check_url
|
73
|
65
|
|
|
66
|
+ def check_url
|
74
|
67
|
stop_query = URI.encode(options["stops"].collect{|a| "&stops=#{a}"}.join)
|
75
|
|
-
|
76
|
|
- u = "http://webservices.nextbus.com/service/publicXMLFeed?command=predictionsForMultiStops&a=#{options["agency"]}#{stop_query}"
|
77
|
|
- log "Fetching #{u}"
|
78
|
|
- u
|
|
68
|
+ "http://webservices.nextbus.com/service/publicXMLFeed?command=predictionsForMultiStops&a=#{options["agency"]}#{stop_query}"
|
79
|
69
|
end
|
|
70
|
+
|
80
|
71
|
def stops
|
81
|
72
|
options["stops"].collect{|a| a.split("|").last}
|
82
|
73
|
end
|
83
|
74
|
def check
|
84
|
|
- puts "*************************\n\n\n\nI get called!!!\n\n\n\n**************************************"
|
85
|
75
|
page = session.get(check_url)
|
86
|
76
|
page = Nokogiri::XML page.body
|
87
|
77
|
predictions = page.css("//prediction")
|
88
|
|
- puts "predictions #{predictions.to_xml}"
|
89
|
|
- puts "minutes #{predictions.collect{|a| a["minutes"]}.join(",")}"
|
90
|
78
|
predictions.each do |pr|
|
91
|
79
|
parent = pr.parent.parent
|
92
|
80
|
vals = {routeTitle: parent["routeTitle"], stopTag: parent["stopTag"]}
|
93
|
|
- if pr["minutes"] && pr["minutes"].to_i < 60
|
|
81
|
+ if pr["minutes"] && pr["minutes"].to_i < options["alert_window_in_minutes"].to_i
|
94
|
82
|
vals = vals.merge Hash.from_xml(pr.to_xml)
|
95
|
83
|
if not_already_in_memory?(vals)
|
96
|
84
|
create_event(:payload => vals)
|